package com.wandian.view.ui
{

	import com.wandian.core.BitmapManager;
	import com.wandian.data.GameData;
	import com.wandian.util.UIHelper;
	import com.wandian.view.layer.BaseLayer;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.utils.describeType;

	import ghostcat.util.easing.TweenUtil;

	/**
	 * 弹出对话层
	 * @author long
	 */
	public class UIPopupLayer extends BaseLayer
	{

		private static var _I:UIPopupLayer=null;
		public var curShowUI:*;
		/**
		 * 正在显示谈话框
		 */
		public var isShow:Boolean=false;

		public var mcLoading:MovieClip;
		public var maskSprite:Sprite;

		public function UIPopupLayer(prv:PrivateClass)
		{
			super();
			_I=this;
		}

		public function showLoading():void
		{
			if (isShow)
			{
				return;
			}
			if (mcLoading)
			{
				if (mcLoading.parent)
				{
					mcLoading.parent.removeChild(mcLoading);
					mcLoading=null;
				}
			}
			this.mcLoading=BitmapManager.instance.getMCByName(GameData.MC_LOADING);
			if (this.mcLoading)
			{
				this.mcLoading.x=stage.stageWidth / 2;
				this.mcLoading.y=stage.stageHeight / 2;
				this.addChild(this.mcLoading);
			}
			Texas.I.gameStage.enabled=false;
		}

		public function hideLoading():void
		{
			if (mcLoading)
			{
				if (mcLoading.parent)
				{
					mcLoading.parent.removeChild(mcLoading);
					mcLoading=null;
				}
			}
			Texas.I.gameStage.enabled=true;
		}

		/**
		 * 隐藏弹出框
		 */
		public function hide():void
		{
			if (curShowUI)
			{
				var xml:XML=describeType(curShowUI);
//				trace(xml.toXMLString());
				for each (var child:XML in xml..method)
				{
					var name:String=child.@name.toString();
					if (name == "destroy")
					{
						curShowUI.destroy();
						break;
					}
				}
//				if (curShowUI.destroy)
//				{
//					curShowUI.destroy();
//				}
//				if (curShowUI.parent)
//				{
//					this.removeChild(this.curShowUI);
//				}
				isShow=false;
				curShowUI=null;
//				if (maskSprite)
//				{
//					maskSprite.parent.removeChild(maskSprite);
//					maskSprite=null;
//				}
			}
		}

		/**
		 * 显示弹出框
		 * @param display  必须继承UILoadingBase 自动加载UI基类
		 *
		 */
		public function show(display:DisplayObject):void
		{

			if (isShow)
				return;

			hide();
			this.addChild(display);
			this.curShowUI=display;
//			this.x=0;
			this.curShowUI.x=(stage.width - this.width) / 2;
			this.curShowUI.y=(Texas.I.gameStage.height - this.height) / 2;
			this.visible=true;
			isShow=true;
//			maskSprite=UIHelper.addRectSpriteChild(this, null, null, 0, 0, display.width ,display.height,0,1);
//			maskSprite.x=(stage.stageWidth - this.width - maskSprite.width) / 2;
//			maskSprite.y=(stage.stageHeight - this.height - maskSprite.height) / 2;
		}

		/**
		 * 第一次进入游戏的任务弹窗
		 *
		 */
		public function firstTaskShow():void
		{
//			var value:Boolean=TaskManager.instance.isFirstGame2Task();
//			if (value)
//			{
//				var npc:MapNPC=RoleManager.instance.findFirstTaskNPC();
//
//				if (!npc || !npc.npcData.taskList || npc.npcData.taskList.length == 0)
//				{
//					trace("没有找到第一任务NPC,或NPC身上的任务列表为空");
//					return;
//				}
//				//第一次任务弹窗
//				var ui:UITaskTalk=new UITaskTalk(npc, true);
//				this.show(ui);
//				isShow=true;
//				GameConst.isFirstGame=false;
//			}
		}

		public static function get I():UIPopupLayer
		{
			if (_I == null)
			{
				_I=new UIPopupLayer(new PrivateClass());
			}
			return _I;
		}

		public function resize():void
		{
			if (stage&&this.curShowUI)
			{
				this.curShowUI.x=(stage.width - this.width) / 2;
				this.curShowUI.y=(stage.height - this.height) / 2;
//				this.curShowUI.x=(stage.stageWidth - this.width) / 2;
//				this.curShowUI.y=(stage.stageHeight - this.height) / 2;
			}
		}

	}
}

class PrivateClass
{
}
